
import { PROTOCAL_SEGMENT_BEGIN, PROTOCAL_SEGMENT_END, PROTOCAL_SEGMENT_SEPARATOR } from './base'

export const PROTOCAL_SEGMENT_IMAGE = '4'

export function toHtml(params, id, readonly) {
  let html = ''
  // + 'style="width:' + params.style.width + 'px;' + 'height: ' + params.style.height + 'px;' + '"
  html = '<img src="' + params.url + '" class="template-col" />'
  return html
}

export function toTemplate(params) {
  return PROTOCAL_SEGMENT_BEGIN + PROTOCAL_SEGMENT_IMAGE + PROTOCAL_SEGMENT_SEPARATOR + JSON.stringify(params) + PROTOCAL_SEGMENT_END
}

export function toData(params) {
}

export function parseImageToHtml(segment, segmentValue, id, readonly) {
  if (!segment) {
    return null
  }
  const data = parseImageToData(segment, segmentValue)
  if (data) {
    return toHtml(data, id, readonly)
  }
  return null
}

export function parseImageToData(segment, segmentValue, id, readonly) {
  if (!segment) {
    return null
  }
  if (segment.startsWith(PROTOCAL_SEGMENT_IMAGE)) {
    // console.log(segment, 'parseImageToHtml')
    const segmentString = segment.substring(PROTOCAL_SEGMENT_IMAGE.length + PROTOCAL_SEGMENT_SEPARATOR.length)
    const data = JSON.parse(segmentString)
    if (segmentValue) {
      data.value = segmentValue
    }
    return data
  }
  return null
}
